package br.com.sigmah.domain.repository;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Component;
import br.com.sigmah.domain.entity.Curso;

@Component
public class CursoDataManager extends AbstractDataManager<Curso,Integer>{

	public CursoDataManager() {
		super(Curso.class);
		// TODO Auto-generated constructor stub
	}

	public void save(Curso curso) {
		getEntityManager().persist(curso);
	}

	@SuppressWarnings("unchecked")
	public List<Curso> list() {
		return super.getAll();
	}

	public void excluir(Curso curso){
		super.remove(curso);
	}
	
	public List<Curso> searchByPredicate(Curso curso){
		CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
		CriteriaQuery<Curso> cq = cb.createQuery(Curso.class);
		Root<Curso> root = cq.from(Curso.class);
		cq.select(root);
		Predicate predicate = cb.like(root.<String>get("nome"), "%"+curso.getNome()+"%");
		cq.where(predicate);
		TypedQuery<Curso>query = getEntityManager().createQuery(cq);
		List<Curso> list = query.getResultList();
		return list;
	}
}
